package com.ruoyi.api.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.LitterBsAreaDTO;
import com.ruoyi.system.domain.dto.LitterBsCityDTO;
import com.ruoyi.system.domain.dto.SysAddressDTO;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/crmarea")
public class CrmAreaController {

    @Autowired
    private ISysAreaService SysAreaServiceImpl;
private  IBsAreaService  bsAreaServiceImpl;
    /**
     * 获取所有的省列表信息
     * @return
     */
    @GetMapping("/getProvince")
    public AjaxResult getProvince(){
        AjaxResult ajax = AjaxResult.success();
        SysArea sysArea=new SysArea();
        sysArea.setLevel(1);
        List<SysArea> bsProvinces = SysAreaServiceImpl.selectSysAreaList(sysArea);
        /*Map<String, List<BsProvince>> bsCityMap = bsProvinces.stream().collect(Collectors.groupingBy(BsProvince::getProvinceInitials));
        List<Map<String,Object>>  result =new ArrayList<>();
        for (String key :bsCityMap.keySet()
        ) {
            List<BsProvince> tmpList = bsCityMap.get(key);
            List<String> cityFinally = tmpList.stream().map(c -> c.getProvinceName()).collect(Collectors.toList());
            Map<String,Object> map =new HashMap<>();
            map.put("letter",key);
            map.put("list",cityFinally);
            result.add(map);
        }
        ajax.put("data",result);*/

        ajax.put("data",bsProvinces);
        return ajax;
    }

    /**
     * 市
     * @return
     */
    @GetMapping("/getCity")
    public AjaxResult getCity(String cityCode){
        AjaxResult ajax = AjaxResult.success();
        SysArea sysArea=new SysArea();
        sysArea.setCityCode(cityCode);
        List<SysArea> bsCities = SysAreaServiceImpl.selectSysAreaList(sysArea);
       /*
        Map<String, List<LitterBsCityDTO>> bsCityMap = bsCities.stream().collect(Collectors.groupingBy(LitterBsCityDTO::getCityInitials));
        List<Map<String,Object>>  result =new ArrayList<>();
        for (String key :bsCityMap.keySet()
        ) {
            List<LitterBsCityDTO> tmpList = bsCityMap.get(key);
            Map<String,Object> map =new HashMap<>();
            map.put("letter",key);
            map.put("list",tmpList);
            result.add(map);
        }
        ajax.put("data",result);*/
        ajax.put("data",bsCities);

        return ajax;
    }

    @GetMapping("/getArea")
    public AjaxResult getArea(String cityCode){
        AjaxResult ajax = AjaxResult.success();
        SysArea sysArea=new SysArea();
        sysArea.setCityCode(cityCode);
        List<SysArea> bsCities = SysAreaServiceImpl.selectSysAreaList(sysArea);
       ajax.put("data",bsCities);
        return ajax;
    }

    @GetMapping("/getStreet")
    public AjaxResult getStreet(String cityCode){
        AjaxResult ajax = AjaxResult.success();
        SysArea sysArea=new SysArea();
        sysArea.setCityCode(cityCode);
        List<SysArea> bsCities = SysAreaServiceImpl.selectSysAreaList(sysArea);
        ajax.put("data",bsCities);
        return ajax;
    }

    @GetMapping("/getAddressBylgla")
    public AjaxResult getAddressBylgla(String lgla){
        AjaxResult ajax = AjaxResult.success();
        BsArea areaByLgla = bsAreaServiceImpl.getAreaByLgla(lgla);
        SysAddressDTO addressCodeByArea = bsAreaServiceImpl.getAddressCodeByArea(areaByLgla);
        ajax.put("data",addressCodeByArea);
        return ajax;
    }
}
